import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy, globalConfig } = window
const { COVIDUrl } = globalConfig
// ===================================================================== $Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
const AuditStatus = [{ name:'全部', value:-99 },{ name:'待审核', value:0 },{ name:'已审核', value:1 },{ name:'已作废', value:-1 }]
// ===================================================================== component
class Index extends React.Component{
	state={
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms: [
			{ label: '单位名称',		name: 'hosp_name',		type: 'select', 	loading:true,	idStr:'hospName',	nameStr:'hospName' },
			{ label: '单位条码',		name: 'hosp_no' },
			{ label: '状态',			name: 'status',			type: 'select', 			data:AuditStatus },
			{ label: '业务区域',		name: 'areaId',			type: 'select', 	loading:true },
			{ label: '业务员',		name: 'salesman_id', 				type: 'select', loading:true },
			{ label: '创建时间',		names: ['check_time_start','check_time_end'], 	type:'date-range', name: 'jz-time'  },
		],
		// 提交表单列表
		formData : [
			{ label: '单位名称',		name: 'hospName', 		half:true,		required:true},
			{ label: '单位条码',		name: 'hospNo', 		half:true,		required:true},
			{ label: '开票单位',		name: 'orgName', 		half:true,		required:true	},
			{ label: '开票税号',		name: 'orgNum', 		half:true,		required:true	},
			{ label: '单位地址',		name: 'hospAddress', 	half:true	},
			{ label: '业务员名称',	name: 'salesman', 		half:true,		required:true	},
			{ label: '业务员编号',	name: 'salesno', 		half:true,		required:true	},
			{ label: '业务区域',		name: 'areaId', 		half:true,		required:true,	type:'select', loading:true	},
			{ label: '单采价格',		name: 'sprice', 		half:true,		required:true	},
			{ label: '混采价格',		name: 'mprice', 		half:true,		required:true	},
			{ label: 'NC单位',		name: 'ncName', 		half:true 	},
			{ label: 'NC单位编码',	name: 'ncNumber', 		half:true 	},
			{ label: '备注',			name: 'remark', 		full:true, 	type:'textarea' },
		]
	}
	
	componentDidMount(){
		this.getSelect()
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
		$cache.then( async f => {
			// 区域
			this.areaData = await f.getCache({ api:'bs_area' } )
			$fn.setRows( [forms, formData], 'areaId', this.areaData )
			// 业务员
			$fn.setRow(forms, 'salesman_id', await f.getCache({ api:'salesman' } ) )
			// 医院
			$fn.setRow( forms, 'hosp_name',  await f.getCache({ api:COVIDUrl+'Hospital/query',param:{page:1,page_num:10000} }))
			//
			this.setState({ forms, formData })
			this.fetch()
		})
	}

	// paging
	fetch = () => $fn.fetch.call(this,COVIDUrl+'Hospital/query', { COVID:true })
		
	// 表格数据
	cols = [
		{ title: '单位名称', 	dataIndex: 'hospName', 			width:200  },
		{ title: '单位条码', 	dataIndex: 'hospNo', 			width:100  },
		{ title: '开票单位', 	dataIndex: 'orgName', 			width:200  },
		{ title: '开票税号', 	dataIndex: 'orgNum', 			width:120  },
		{ title: '单位地址', 	dataIndex: 'hospAddress',  		width:200  },
		{ title: '状态', 		dataIndex: 'status', 			 width:100,		render: text => $fn.getState(AuditStatus,text) },
		{ title: '业务员名称', 	dataIndex: 'salesman',  		...$fn.table.name  },
		{ title: '业务员编号', 	dataIndex: 'salesno',  			width:120  },
		{ title: '业务区域', 	dataIndex: 'areaId',  			width:150,			render: text => $fn.getState(this.areaData,text)  },
		{ title: '创建时间', 	dataIndex: 'createdAt',  		...$fn.table.time  },
		{ title: 'NC单位',		dataIndex: 'ncName',					width:150 },
		{ title: 'NC单位编码',	dataIndex: 'ncNumber',					width:100  },
		{ title: '单采价格',		dataIndex: 'sprice',			width:100 },
		{ title: '混采价格',		dataIndex: 'mprice',			width:100  },
		{ title: '描述', 		dataIndex: 'remark',  			width:200  },
		TableControls.controls.call(this,{ width: 100 })
	]
	table_controls = ({uuid, status}) => {
		return [
			TableControls.edit.call(this, COVIDUrl+'Hospital/getrow', { uuid }),
			+status === 0 && Confirm.call(this, '审核', COVIDUrl+'Hospital/status', { query:{uuids:uuid,status:1},onComplete:()=>this.clearHosp() }),
			+status === 1 && Confirm.call(this, '作废', COVIDUrl+'Hospital/status', { query:{uuids:uuid,status:-1},onComplete:()=>this.clearHosp() }),
		]
	}
	
	// 操作按钮
	controls = () => {
		const { selectedKeys } = this.state
		const uuids = selectedKeys.join(',')
		return [
			Controls.add.call(this),					// 新增
			Confirm.call(this, '审核', COVIDUrl+'Hospital/status', { isKeys:1, query:{uuids,status:1},onComplete:()=>this.clearHosp() }),
			Confirm.call(this, '作废', COVIDUrl+'Hospital/status', { isKeys:1, query:{uuids,status:-1},onComplete:()=>this.clearHosp() }),
		]
	}
	clearHosp = () => $fn.dataRemove(COVIDUrl+'Hospital/query')
	
	render(){
		return (
			<>
				<Page title='送检单位' controls={this.controls()} >
					{/* 搜索 */}
					{ SearchForm.call(this, {timeStr:''}) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加 编辑 */}
				{AddEditModal.call(this,{title:'送检单位', labelWidth:'s6', width:800, api:COVIDUrl+'Hospital/create', editApi:COVIDUrl+'Hospital/update',
					onComplete:()=>this.clearHosp()
				})}
			</>
		)
	}
}
export default Index